package com.qdlc.p2p.biz.service.impl;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.EnjoyGoodsService;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.dao.EnjoyBorrowDao;
import com.qdlc.p2p.dal.dao.EnjoyGoodsDao;
import com.qdlc.p2p.dal.dao.EnjoyUploadDao;
import com.qdlc.p2p.dal.dto.EnjoyGoods;
import com.qdlc.p2p.dal.dto.EnjoyUpload;
import com.qdlc.p2p.dal.model.EnjoyGoodsModel;

/**
 * Created by D on 2016/1/20.
 */
@Service("enjoyGoodsService")
public class EnjoyGoodsServiceImpl implements EnjoyGoodsService {

    @Resource
    private EnjoyGoodsDao enjoyGoodsDao;

    @Resource
    private EnjoyUploadDao enjoyUploadDao;

    @Resource
    private EnjoyBorrowDao enjoyBorrowDao;

    @Override
    public List<EnjoyGoods> findUsableEnjoyGoods() {
        return enjoyGoodsDao.findByProperty("status", EnjoyGoodsModel.ENJOY_GOODS_ENABLE);
    }

    @Override
    public PageDataList<EnjoyGoods> findByModel(EnjoyGoodsModel model) {
        QueryParam param = QueryParam.getInstance();
        if(StringUtil.isNotBlank(model.getSearchName())){//模糊查询条件
            param.addParam("name", SearchFilter.Operators.LIKE , model.getSearchName());
        } else {
            if (model.getStatus() != 0) {
                param.addParam("status", model.getStatus());
            }
        }
        param.addPage(model.getPage(), model.getRows());
        param.addOrder(OrderFilter.OrderType.DESC, "id");
        PageDataList<EnjoyGoods> pageDataList = enjoyGoodsDao.findPageList(param);
        return pageDataList;
    }

    @Override
    public void save(EnjoyGoods enjoyGoods,List<EnjoyUpload> list) {
        EnjoyGoods addEnjoyGoods=enjoyGoodsDao.save(enjoyGoods);
        List<EnjoyUpload> l = new ArrayList<EnjoyUpload>();
        for (EnjoyUpload eu : list) {
            eu.setGoodsId(addEnjoyGoods.getId());
            l.add(eu);
        }
        enjoyUploadDao.save(l);
    }

    @Override
    public EnjoyGoods findById(long id) {
        return enjoyGoodsDao.find(id);
    }

    @Override
    public void update(EnjoyGoods enjoyGoods,List<EnjoyUpload> list,long[] delIds) {
        enjoyGoodsDao.update(enjoyGoods);
        if (delIds != null) {
            for (long id : delIds) {
                EnjoyUpload eu = enjoyUploadDao.find(id);
                enjoyUploadDao.delete(id);
                String realPath = ServletActionContext.getServletContext().getRealPath(eu.getPicPath());
                if (StringUtil.isNotBlank(realPath)) {
                    new File(realPath).delete();
                }
            }
        }
        enjoyUploadDao.save(list);
    }

    @Override
    public List<EnjoyUpload> findPicByGoodsId(long id) {
        QueryParam param = QueryParam.getInstance().addParam("goodsId", id);
        return enjoyUploadDao.findByCriteria(param);
    }

    @Override
    public List<Integer> enablePosition(int prefecture) {
        List disablePosition=enjoyGoodsDao.disablePosition(prefecture);
        List<Integer> enablePosition=new ArrayList<Integer>();
        for(int i=1;i<9;i++){
            if(!disablePosition.contains(i))
                enablePosition.add(i);
        }
        enablePosition.add(9);
        return enablePosition;
    }

    @Override
    public List<Integer> enablePosition(int prefecture, long enjoyGoodsId) {
        List<Integer> enablePosition=enablePosition(prefecture);
        EnjoyGoods eg=enjoyGoodsDao.find(enjoyGoodsId);
        if(prefecture==eg.getPrefecture()&&(!enablePosition.contains(eg.getPosition())))
            enablePosition.add(eg.getPosition());
        return enablePosition;
    }

    @Override
    public boolean isBorrow(long goodsId) {
        boolean flag=false;
        QueryParam param = QueryParam.getInstance();
        param.addParam("enjoyGoods.id",goodsId);
        int count=enjoyBorrowDao.countByCriteria(param);
        if(count>0)
            flag=true;
        return flag;
    }
}
